package io.afu.baseframework.utils;


import io.afu.baseframework.exceptions.BaseException;
import io.afu.common.constant.FileConstant;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {


    /**
     * 获取带后缀的文件名
     * @param filePath 文件路径
     * @return String
     */
    public static String getFileNameByFilePath(String filePath){
        String fileName = null;
        if (filePath.contains("\\")){
            fileName =filePath.substring(filePath.lastIndexOf("\\")+1,filePath.length());
        }else {
            fileName = filePath.substring(filePath.lastIndexOf("/")+1,filePath.length());
        }
        return fileName;
    }

    /**
     * 获取不带后缀的文件名
     * @param filePath 文件路径
     * @return String
     */
    public static String getFileNameWithoutSuffix(String filePath) {
        String nameWithoutSuffix = null;
        String fileName = getFileNameByFilePath(filePath);
        nameWithoutSuffix = fileName.substring(0,fileName.lastIndexOf("."));
        return nameWithoutSuffix;
    }

    public static void deleteFile(File file)  {
        if (file.exists()) {
            if (file.isFile()) {
                file.deleteOnExit();
            }else {
                File[] files = file.listFiles();
                for (File file1:files) {
                    deleteFile(file1);
                }
                file.deleteOnExit();
            }
        }
    }


    /**
     * 通过URL来获取文件后缀
     * @param url 文件url
     * @return 后缀名
     */
    public static String getFileNameByUrl(String url){
        String[] strs = url.split("\\?");
        String pureUrl = strs[0];
        return pureUrl.substring(pureUrl.lastIndexOf("/")+1,pureUrl.length());
    }

    /**
     * 获取文件的二进制代码
     * @param filePath 文件路径
     * @return 返回二进制数组
     * @throws BaseException 文件读取出错抛错
     */
    public static byte[] getBytesFromFilePath(String filePath) throws BaseException {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fileInputStream = new FileInputStream(file);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n=fileInputStream.read(b))!=-1){
                byteArrayOutputStream.write(b,0,n);
            }
            fileInputStream.close();
            byteArrayOutputStream.close();
            buffer = byteArrayOutputStream.toByteArray();
        }catch (Exception e){
            throw new BaseException(e);
        }
        return buffer;
    }

    /**
     * 写入bytes 到文件
     * @param filePath 文件路径
     * @param bytes 字节数组
     * @param add 是否追加
     * @throws BaseException 抛错
     */
    public static void writeBytesToFile(String filePath,byte[] bytes,Boolean add) throws BaseException {
        try (FileOutputStream fileOutputStream = new FileOutputStream(filePath,add)){
            fileOutputStream.write(bytes);
        }catch (Exception e){
            throw new BaseException(e);
        }
    }

    /**
     * 写入String
     * @param filePath 路径
     * @param toWrite 需要写入的String
     * @throws BaseException 抛错
     */
    public static void writeStringToFile(String filePath,String toWrite) throws BaseException {
        byte[] toWriteArray = toWrite.getBytes();
        writeBytesToFile(filePath,toWriteArray,false);
    }

    /**
     * 写入String
     * @param filePath 路径
     * @param toWrite 需要写入的String
     * @param add 是否追加到文件中
     * @throws BaseException 抛错
     */
    public static void writeStringToFile(String filePath,String toWrite,Boolean add) throws BaseException {
        byte[] toWriteArray = toWrite.getBytes();
        writeBytesToFile(filePath,toWriteArray,add);
    }

    /**
     * 获取文件的后缀
     * @param fileNameOrPath 文件名或者文件完整路径
     * @return 后缀
     */
    public static String getFileSuffix(String fileNameOrPath){
        if (fileNameOrPath == null){
            return "";
        }
        String[] strs = fileNameOrPath.split("\\.");
        return strs[strs.length-1];
    }

    public static String getFileSuffixSubstringWay(String fileName){
        String suffix = "";
        int index = fileName.lastIndexOf(".");
        if (index != -1){
            suffix = fileName.substring(index+1);
        }
        return suffix;
    }

    /**
     * 判断文件是否存在
     * @param filePath 文件路径
     * @return 是否存在的布尔值
     * @throws BaseException 抛错
     */
    public static Boolean exist(String filePath) throws BaseException {
        File file = new File(filePath);
        return file.exists();
    }

    /**
     * 获取文件类型
     * @param fileNameOrPath 文件路径或者文件名称
     * @return 文件类型
     */
    public static String getFileType(String fileNameOrPath) {
        if (fileNameOrPath == null){
            return "";
        }
        String suffix = getFileSuffix(fileNameOrPath);
        for (String vedio: FileConstant.VEDIO_TYPE){
            if (suffix.trim().toUpperCase().equals(vedio.toUpperCase())){
                return FileConstant.TYPE_VEDIO;
            }
        }
        for (String audio:FileConstant.AUDIO_TYPE){
            if (suffix.trim().toUpperCase().equals(audio.toUpperCase())){
                return FileConstant.TYPE_AUDIO;
            }
        }
        for (String img:FileConstant.IMAGE_TYPE){
            if (suffix.trim().toUpperCase().equals(img.toUpperCase())){
                return FileConstant.TYPE_IMAGE;
            }
        }
        return FileConstant.TYPE_TEXT_FILE;
    }

    public static String getFileContentTypeBySuffix(String suffix){
        if ("tif".toLowerCase().equals(suffix)){
            return "image/tiff";
        }

        if ("001".toLowerCase().equals(suffix)){
            return "application/x-001";
        }

        if ("301".toLowerCase().equals(suffix)){
            return "application/x-301";
        }

        if ("323".toLowerCase().equals(suffix)){
            return "text/h323";
        }

        if ("906".toLowerCase().equals(suffix)){
            return "application/x-906";
        }

        if ("907".toLowerCase().equals(suffix)){
            return "drawing/907";
        }

        if ("a11".toLowerCase().equals(suffix)){
            return "application/x-a11";
        }

        if ("acp".toLowerCase().equals(suffix)){
            return "audio/x-mei-aac";
        }

        if ("ai".toLowerCase().equals(suffix)){
            return "application/postscript";
        }

        if ("aif".toLowerCase().equals(suffix)){
            return "audio/aiff";
        }

        if ("aifc".toLowerCase().equals(suffix)){
            return "audio/aiff";
        }

        if ("aiff".toLowerCase().equals(suffix)){
            return "audio/aiff";
        }

        if ("anv".toLowerCase().equals(suffix)){
            return "application/x-anv";
        }

        if ("asa".toLowerCase().equals(suffix)){
            return "text/asa";
        }

        if ("asf".toLowerCase().equals(suffix)){
            return "video/x-ms-asf";
        }

        if ("asp".toLowerCase().equals(suffix)){
            return "text/asp";
        }

        if ("asx".toLowerCase().equals(suffix)){
            return "video/x-ms-asf";
        }

        if ("au".toLowerCase().equals(suffix)){
            return "audio/basic";
        }

        if ("avi".toLowerCase().equals(suffix)){
            return "video/avi";
        }

        if ("awf".toLowerCase().equals(suffix)){
            return "application/vnd.adobe.workflow";
        }

        if ("biz".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("bmp".toLowerCase().equals(suffix)){
            return "application/x-bmp";
        }

        if ("bot".toLowerCase().equals(suffix)){
            return "application/x-bot";
        }

        if ("c4t".toLowerCase().equals(suffix)){
            return "application/x-c4t";
        }

        if ("c90".toLowerCase().equals(suffix)){
            return "application/x-c90";
        }

        if ("cal".toLowerCase().equals(suffix)){
            return "application/x-cals";
        }

        if ("cat".toLowerCase().equals(suffix)){
            return "application/vnd.ms-pki.seccat";
        }

        if ("cdf".toLowerCase().equals(suffix)){
            return "application/x-netcdf";
        }

        if ("cdr".toLowerCase().equals(suffix)){
            return "application/x-cdr";
        }

        if ("cel".toLowerCase().equals(suffix)){
            return "application/x-cel";
        }

        if ("cer".toLowerCase().equals(suffix)){
            return "application/x-x509-ca-cert";
        }

        if ("cg4".toLowerCase().equals(suffix)){
            return "application/x-g4";
        }

        if ("cgm".toLowerCase().equals(suffix)){
            return "application/x-cgm";
        }

        if ("cit".toLowerCase().equals(suffix)){
            return "application/x-cit";
        }

        if ("class".toLowerCase().equals(suffix)){
            return "java/*";
        }

        if ("cml".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("cmp".toLowerCase().equals(suffix)){
            return "application/x-cmp";
        }

        if ("cmx".toLowerCase().equals(suffix)){
            return "application/x-cmx";
        }

        if ("cot".toLowerCase().equals(suffix)){
            return "application/x-cot";
        }

        if ("crl".toLowerCase().equals(suffix)){
            return "application/pkix-crl";
        }

        if ("crt".toLowerCase().equals(suffix)){
            return "application/x-x509-ca-cert";
        }

        if ("csi".toLowerCase().equals(suffix)){
            return "application/x-csi";
        }

        if ("css".toLowerCase().equals(suffix)){
            return "text/css";
        }

        if ("cut".toLowerCase().equals(suffix)){
            return "application/x-cut";
        }

        if ("dbf".toLowerCase().equals(suffix)){
            return "application/x-dbf";
        }

        if ("dbm".toLowerCase().equals(suffix)){
            return "application/x-dbm";
        }

        if ("dbx".toLowerCase().equals(suffix)){
            return "application/x-dbx";
        }

        if ("dcd".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("dcx".toLowerCase().equals(suffix)){
            return "application/x-dcx";
        }

        if ("der".toLowerCase().equals(suffix)){
            return "application/x-x509-ca-cert";
        }

        if ("dgn".toLowerCase().equals(suffix)){
            return "application/x-dgn";
        }

        if ("dib".toLowerCase().equals(suffix)){
            return "application/x-dib";
        }

        if ("dll".toLowerCase().equals(suffix)){
            return "application/x-msdownload";
        }

        if ("doc".toLowerCase().equals(suffix)){
            return "application/msword";
        }

        if ("dot".toLowerCase().equals(suffix)){
            return "application/msword";
        }

        if ("drw".toLowerCase().equals(suffix)){
            return "application/x-drw";
        }

        if ("dtd".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("dwf".toLowerCase().equals(suffix)){
            return "application/x-dwf";
        }

        if ("dwg".toLowerCase().equals(suffix)){
            return "application/x-dwg";
        }

        if ("dxb".toLowerCase().equals(suffix)){
            return "application/x-dxb";
        }

        if ("dxf".toLowerCase().equals(suffix)){
            return "application/x-dxf";
        }

        if ("edn".toLowerCase().equals(suffix)){
            return "application/vnd.adobe.edn";
        }

        if ("emf".toLowerCase().equals(suffix)){
            return "application/x-emf";
        }

        if ("eml".toLowerCase().equals(suffix)){
            return "message/rfc822";
        }

        if ("ent".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("epi".toLowerCase().equals(suffix)){
            return "application/x-epi";
        }

        if ("eps".toLowerCase().equals(suffix)){
            return "application/x-ps";
        }

        if ("etd".toLowerCase().equals(suffix)){
            return "application/x-ebx";
        }

        if ("exe".toLowerCase().equals(suffix)){
            return "application/x-msdownload";
        }

        if ("fax".toLowerCase().equals(suffix)){
            return "image/fax";
        }

        if ("fdf".toLowerCase().equals(suffix)){
            return "application/vnd.fdf";
        }

        if ("fif".toLowerCase().equals(suffix)){
            return "application/fractals";
        }

        if ("fo".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("frm".toLowerCase().equals(suffix)){
            return "application/x-frm";
        }

        if ("g4".toLowerCase().equals(suffix)){
            return "application/x-g4";
        }

        if ("gbr".toLowerCase().equals(suffix)){
            return "application/x-gbr";
        }

        if ("".toLowerCase().equals(suffix)){
            return "application/x-";
        }

        if ("gif".toLowerCase().equals(suffix)){
            return "image/gif";
        }

        if ("gl2".toLowerCase().equals(suffix)){
            return "application/x-gl2";
        }

        if ("gp4".toLowerCase().equals(suffix)){
            return "application/x-gp4";
        }

        if ("hgl".toLowerCase().equals(suffix)){
            return "application/x-hgl";
        }

        if ("hmr".toLowerCase().equals(suffix)){
            return "application/x-hmr";
        }

        if ("hpg".toLowerCase().equals(suffix)){
            return "application/x-hpgl";
        }

        if ("hpl".toLowerCase().equals(suffix)){
            return "application/x-hpl";
        }

        if ("hqx".toLowerCase().equals(suffix)){
            return "application/mac-binhex40";
        }

        if ("hrf".toLowerCase().equals(suffix)){
            return "application/x-hrf";
        }

        if ("hta".toLowerCase().equals(suffix)){
            return "application/hta";
        }

        if ("htc".toLowerCase().equals(suffix)){
            return "text/x-component";
        }

        if ("htm".toLowerCase().equals(suffix)){
            return "text/html";
        }

        if ("html".toLowerCase().equals(suffix)){
            return "text/html";
        }

        if ("htt".toLowerCase().equals(suffix)){
            return "text/webviewhtml";
        }

        if ("htx".toLowerCase().equals(suffix)){
            return "text/html";
        }

        if ("icb".toLowerCase().equals(suffix)){
            return "application/x-icb";
        }

        if ("ico".toLowerCase().equals(suffix)){
            return "image/x-icon";
        }


        if ("iff".toLowerCase().equals(suffix)){
            return "application/x-iff";
        }

        if ("ig4".toLowerCase().equals(suffix)){
            return "application/x-g4";
        }

        if ("igs".toLowerCase().equals(suffix)){
            return "application/x-igs";
        }

        if ("iii".toLowerCase().equals(suffix)){
            return "application/x-iphone";
        }

        if ("img".toLowerCase().equals(suffix)){
            return "application/x-img";
        }

        if ("ins".toLowerCase().equals(suffix)){
            return "application/x-internet-signup";
        }

        if ("isp".toLowerCase().equals(suffix)){
            return "application/x-internet-signup";
        }

        if ("IVF".toLowerCase().equals(suffix)){
            return "video/x-ivf";
        }

        if ("java".toLowerCase().equals(suffix)){
            return "java/*";
        }

        if ("jfif".toLowerCase().equals(suffix)){
            return "image/jpeg";
        }

        if ("jpe".toLowerCase().equals(suffix)){
            return "image/jpeg";
        }


        if ("jpeg".toLowerCase().equals(suffix)){
            return "image/jpeg";
        }

        if ("jpg".toLowerCase().equals(suffix)){
            return "image/jpeg";
        }

        if ("js".toLowerCase().equals(suffix)){
            return "application/x-javascript";
        }

        if ("jsp".toLowerCase().equals(suffix)){
            return "text/html";
        }

        if ("la1".toLowerCase().equals(suffix)){
            return "audio/x-liquid-file";
        }

        if ("lar".toLowerCase().equals(suffix)){
            return "application/x-laplayer-reg";
        }

        if ("latex".toLowerCase().equals(suffix)){
            return "application/x-latex";
        }

        if ("lavs".toLowerCase().equals(suffix)){
            return "audio/x-liquid-secure";
        }

        if ("lbm".toLowerCase().equals(suffix)){
            return "application/x-lbm";
        }

        if ("lmsff".toLowerCase().equals(suffix)){
            return "audio/x-la-lms";
        }

        if ("ls".toLowerCase().equals(suffix)){
            return "application/x-javascript";
        }

        if ("ltr".toLowerCase().equals(suffix)){
            return "application/x-ltr";
        }

        if ("m1v".toLowerCase().equals(suffix)){
            return "video/x-mpeg";
        }

        if ("m2v".toLowerCase().equals(suffix)){
            return "video/x-mpeg";
        }

        if ("m3u".toLowerCase().equals(suffix)){
            return "audio/mpegurl";
        }

        if ("m4e".toLowerCase().equals(suffix)){
            return "video/mpeg4";
        }

        if ("mac".toLowerCase().equals(suffix)){
            return "application/x-mac";
        }

        if ("man".toLowerCase().equals(suffix)){
            return "application/x-troff-man";
        }

        if ("math".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("mdb".toLowerCase().equals(suffix)){
            return "application/x-mdb";
        }

        if ("mfp".toLowerCase().equals(suffix)){
            return "application/x-shockwave-flash";
        }

        if ("mht".toLowerCase().equals(suffix)){
            return "message/rfc822";
        }

        if ("mhtml".toLowerCase().equals(suffix)){
            return "message/rfc822";
        }

        if ("mi".toLowerCase().equals(suffix)){
            return "application/x-mi";
        }

        if ("mid".toLowerCase().equals(suffix)){
            return "audio/mid";
        }

        if ("midi".toLowerCase().equals(suffix)){
            return "audio/mid";
        }

        if ("mil".toLowerCase().equals(suffix)){
            return "application/x-mil";
        }

        if ("mml".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("mnd".toLowerCase().equals(suffix)){
            return "audio/x-musicnet-download";
        }

        if ("mns".toLowerCase().equals(suffix)){
            return "audio/x-musicnet-stream";
        }

        if ("mocha".toLowerCase().equals(suffix)){
            return "application/x-javascript";
        }

        if ("movie".toLowerCase().equals(suffix)){
            return "video/x-sgi-movie";
        }

        if ("mp1".toLowerCase().equals(suffix)){
            return "audio/mp1";
        }

        if ("mp2".toLowerCase().equals(suffix)){
            return "audio/mp2";
        }

        if ("mp2v".toLowerCase().equals(suffix)){
            return "video/mpeg";
        }

        if ("mp3".toLowerCase().equals(suffix)){
            return "audio/mp3";
        }

        if ("mp4".toLowerCase().equals(suffix)){
            return "video/mp4";
        }

        if ("mpa".toLowerCase().equals(suffix)){
            return "video/x-mpg";
        }

        if ("mpd".toLowerCase().equals(suffix)){
            return "application/vnd.ms-project";
        }

        if ("mpe".toLowerCase().equals(suffix)){
            return "video/x-mpeg";
        }

        if ("mpeg".toLowerCase().equals(suffix)){
            return "video/mpg";
        }

        if ("mpg".toLowerCase().equals(suffix)){
            return "video/mpg";
        }

        if ("mpga".toLowerCase().equals(suffix)){
            return "audio/rn-mpeg";
        }

        if ("mpp".toLowerCase().equals(suffix)){
            return "application/vnd.ms-project";
        }

        if ("mps".toLowerCase().equals(suffix)){
            return "video/x-mpeg";
        }

        if ("mpt".toLowerCase().equals(suffix)){
            return "application/vnd.ms-project";
        }

        if ("mpv".toLowerCase().equals(suffix)){
            return "video/mpg";
        }

        if ("mpv2".toLowerCase().equals(suffix)){
            return "video/mpeg";
        }

        if ("mpw".toLowerCase().equals(suffix)){
            return "application/vnd.ms-project";
        }

        if ("mpx".toLowerCase().equals(suffix)){
            return "application/vnd.ms-project";
        }

        if ("mtx".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("mxp".toLowerCase().equals(suffix)){
            return "application/x-mmxp";
        }

        if ("net".toLowerCase().equals(suffix)){
            return "image/pnetvue";
        }

        if ("nrf".toLowerCase().equals(suffix)){
            return "application/x-nrf";
        }

        if ("nws".toLowerCase().equals(suffix)){
            return "message/rfc822";
        }

        if ("odc".toLowerCase().equals(suffix)){
            return "text/x-ms-odc";
        }

        if ("out".toLowerCase().equals(suffix)){
            return "application/x-out";
        }

        if ("p10".toLowerCase().equals(suffix)){
            return "application/pkcs10";
        }

        if ("p12".toLowerCase().equals(suffix)){
            return "application/x-pkcs12";
        }

        if ("p7b".toLowerCase().equals(suffix)){
            return "application/x-pkcs7-certificates";
        }

        if ("p7c".toLowerCase().equals(suffix)){
            return "application/pkcs7-mime";
        }

        if ("p7m".toLowerCase().equals(suffix)){
            return "application/pkcs7-mime";
        }

        if ("p7r".toLowerCase().equals(suffix)){
            return "application/x-pkcs7-certreqresp";
        }

        if ("p7s".toLowerCase().equals(suffix)){
            return "application/pkcs7-signature";
        }

        if ("pc5".toLowerCase().equals(suffix)){
            return "application/x-pc5";
        }

        if ("pci".toLowerCase().equals(suffix)){
            return "application/x-pci";
        }

        if ("pcl".toLowerCase().equals(suffix)){
            return "application/x-pcl";
        }

        if ("pcx".toLowerCase().equals(suffix)){
            return "application/x-pcx";
        }

        if ("pdf".toLowerCase().equals(suffix)){
            return "application/pdf";
        }

        if ("pdx".toLowerCase().equals(suffix)){
            return "application/vnd.adobe.pdx";
        }

        if ("pfx".toLowerCase().equals(suffix)){
            return "application/x-pkcs12";
        }

        if ("pgl".toLowerCase().equals(suffix)){
            return "application/x-pgl";
        }

        if ("pic".toLowerCase().equals(suffix)){
            return "application/x-pic";
        }

        if ("pko".toLowerCase().equals(suffix)){
            return "application/vnd.ms-pki.pko";
        }

        if ("pl".toLowerCase().equals(suffix)){
            return "application/x-perl";
        }

        if ("plg".toLowerCase().equals(suffix)){
            return "text/html";
        }

        if ("pls".toLowerCase().equals(suffix)){
            return "audio/scpls";
        }

        if ("plt".toLowerCase().equals(suffix)){
            return "application/x-plt";
        }

        if ("png".toLowerCase().equals(suffix)){
            return "image/png";
        }

        if ("pot".toLowerCase().equals(suffix)){
            return "application/vnd.ms-powerpoint";
        }

        if ("ppa".toLowerCase().equals(suffix)){
            return "application/vnd.ms-powerpoint";
        }

        if ("ppm".toLowerCase().equals(suffix)){
            return "application/x-ppm";
        }

        if ("pps".toLowerCase().equals(suffix)){
            return "application/vnd.ms-powerpoint";
        }

        if ("ppt".toLowerCase().equals(suffix)){
            return "application/x-ppt";
        }

        if ("pr".toLowerCase().equals(suffix)){
            return "application/x-pr";
        }

        if ("prf".toLowerCase().equals(suffix)){
            return "application/pics-rules";
        }

        if ("prn".toLowerCase().equals(suffix)){
            return "application/x-prn";
        }

        if ("prt".toLowerCase().equals(suffix)){
            return "application/x-prt";
        }

        if ("ps".toLowerCase().equals(suffix)){
            return "application/x-ps";
        }

        if ("ptn".toLowerCase().equals(suffix)){
            return "application/x-ptn";
        }

        if ("pwz".toLowerCase().equals(suffix)){
            return "application/vnd.ms-powerpoint";
        }

        if ("r3t".toLowerCase().equals(suffix)){
            return "text/vnd.rn-realtext3d";
        }

        if ("ra".toLowerCase().equals(suffix)){
            return "audio/vnd.rn-realaudio";
        }

        if ("ram".toLowerCase().equals(suffix)){
            return "audio/x-pn-realaudio";
        }

        if ("ras".toLowerCase().equals(suffix)){
            return "application/x-ras";
        }

        if ("rat".toLowerCase().equals(suffix)){
            return "application/rat-file";
        }

        if ("rdf".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("rec".toLowerCase().equals(suffix)){
            return "application/vnd.rn-recording";
        }

        if ("red".toLowerCase().equals(suffix)){
            return "application/x-red";
        }

        if ("rgb".toLowerCase().equals(suffix)){
            return "application/x-rgb";
        }

        if ("rjs".toLowerCase().equals(suffix)){
            return "application/vnd.rn-realsystem-rjs";
        }

        if ("rjt".toLowerCase().equals(suffix)){
            return "application/vnd.rn-realsystem-rjt";
        }

        if ("rlc".toLowerCase().equals(suffix)){
            return "application/x-rlc";
        }

        if ("rle".toLowerCase().equals(suffix)){
            return "application/x-rle";
        }

        if ("rm".toLowerCase().equals(suffix)){
            return "application/vnd.rn-realmedia";
        }

        if ("rmf".toLowerCase().equals(suffix)){
            return "application/vnd.adobe.rmf";
        }

        if ("rmi".toLowerCase().equals(suffix)){
            return "audio/mid";
        }

        if ("rmj".toLowerCase().equals(suffix)){
            return "application/vnd.rn-realsystem-rmj";
        }

        if ("rmm".toLowerCase().equals(suffix)){
            return "audio/x-pn-realaudio";
        }

        if ("rmp".toLowerCase().equals(suffix)){
            return "application/vnd.rn-rn_music_package";
        }

        if ("rms".toLowerCase().equals(suffix)){
            return "application/vnd.rn-realmedia-secure";
        }

        if ("rmvb".toLowerCase().equals(suffix)){
            return "application/vnd.rn-realmedia-vbr";
        }

        if ("rmx".toLowerCase().equals(suffix)){
            return "application/vnd.rn-realsystem-rmx";
        }

        if ("rnx".toLowerCase().equals(suffix)){
            return "application/vnd.rn-realplayer";
        }

        if ("rp".toLowerCase().equals(suffix)){
            return "image/vnd.rn-realpix";
        }

        if ("rpm".toLowerCase().equals(suffix)){
            return "audio/x-pn-realaudio-plugin";
        }

        if ("rsml".toLowerCase().equals(suffix)){
            return "application/vnd.rn-rsml";
        }

        if ("rt".toLowerCase().equals(suffix)){
            return "text/vnd.rn-realtext";
        }

        if ("rtf".toLowerCase().equals(suffix)){
            return "application/x-rtf";
        }

        if ("rv".toLowerCase().equals(suffix)){
            return "video/vnd.rn-realvideo";
        }

        if ("sam".toLowerCase().equals(suffix)){
            return "application/x-sam";
        }

        if ("sat".toLowerCase().equals(suffix)){
            return "application/x-sat";
        }

        if ("sdp".toLowerCase().equals(suffix)){
            return "application/sdp";
        }

        if ("sdw".toLowerCase().equals(suffix)){
            return "application/x-sdw";
        }

        if ("sit".toLowerCase().equals(suffix)){
            return "application/x-stuffit";
        }

        if ("slb".toLowerCase().equals(suffix)){
            return "application/x-slb";
        }

        if ("sld".toLowerCase().equals(suffix)){
            return "application/x-sld";
        }

        if ("slk".toLowerCase().equals(suffix)){
            return "drawing/x-slk";
        }

        if ("smi".toLowerCase().equals(suffix)){
            return "application/smil";
        }

        if ("smil".toLowerCase().equals(suffix)){
            return "application/smil";
        }

        if ("smk".toLowerCase().equals(suffix)){
            return "application/x-smk";
        }

        if ("snd".toLowerCase().equals(suffix)){
            return "audio/basic";
        }

        if ("sol".toLowerCase().equals(suffix)){
            return "text/plain";
        }

        if ("sor".toLowerCase().equals(suffix)){
            return "text/plain";
        }

        if ("spc".toLowerCase().equals(suffix)){
            return "application/x-pkcs7-certificates";
        }

        if ("spl".toLowerCase().equals(suffix)){
            return "application/futuresplash";
        }

        if ("spp".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("ssm".toLowerCase().equals(suffix)){
            return "application/streamingmedia";
        }

        if ("sst".toLowerCase().equals(suffix)){
            return "application/vnd.ms-pki.certstore";
        }

        if ("stl".toLowerCase().equals(suffix)){
            return "application/vnd.ms-pki.stl";
        }

        if ("stm".toLowerCase().equals(suffix)){
            return "text/html";
        }

        if ("sty".toLowerCase().equals(suffix)){
            return "application/x-sty";
        }

        if ("svg".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("swf".toLowerCase().equals(suffix)){
            return "application/x-shockwave-flash";
        }

        if ("tdf".toLowerCase().equals(suffix)){
            return "application/x-tdf";
        }

        if ("tg4".toLowerCase().equals(suffix)){
            return "application/x-tg4";
        }

        if ("tga".toLowerCase().equals(suffix)){
            return "application/x-tga";
        }

        if ("tif".toLowerCase().equals(suffix)){
            return "image/tiff";
        }

        if ("tiff".toLowerCase().equals(suffix)){
            return "image/tiff";
        }

        if ("tld".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("top".toLowerCase().equals(suffix)){
            return "drawing/x-top";
        }

        if ("torrent".toLowerCase().equals(suffix)){
            return "application/x-bittorrent";
        }

        if ("tsd".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("txt".toLowerCase().equals(suffix)){
            return "text/plain";
        }

        if ("uin".toLowerCase().equals(suffix)){
            return "application/x-icq";
        }

        if ("uls".toLowerCase().equals(suffix)){
            return "text/iuls";
        }

        if ("vcf".toLowerCase().equals(suffix)){
            return "text/x-vcard";
        }

        if ("vda".toLowerCase().equals(suffix)){
            return "application/x-vda";
        }

        if ("vdx".toLowerCase().equals(suffix)){
            return "application/vnd.visio";
        }

        if ("vml".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("vpg".toLowerCase().equals(suffix)){
            return "application/x-vpeg005";
        }

        if ("vsd".toLowerCase().equals(suffix)){
            return "application/x-vsd";
        }

        if ("vss".toLowerCase().equals(suffix)){
            return "application/vnd.visio";
        }

        if ("vst".toLowerCase().equals(suffix)){
            return "application/x-vst";
        }

        if ("vsw".toLowerCase().equals(suffix)){
            return "application/vnd.visio";
        }

        if ("vsx".toLowerCase().equals(suffix)){
            return "application/vnd.visio";
        }

        if ("vtx".toLowerCase().equals(suffix)){
            return "application/vnd.visio";
        }

        if ("vxml".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("wav".toLowerCase().equals(suffix)){
            return "audio/wav";
        }

        if ("wax".toLowerCase().equals(suffix)){
            return "audio/x-ms-wax";
        }

        if ("wb1".toLowerCase().equals(suffix)){
            return "application/x-wb1";
        }

        if ("wb2".toLowerCase().equals(suffix)){
            return "application/x-wb2";
        }

        if ("wb3".toLowerCase().equals(suffix)){
            return "application/x-wb3";
        }

        if ("wbmp".toLowerCase().equals(suffix)){
            return "image/vnd.wap.wbmp";
        }

        if ("wiz".toLowerCase().equals(suffix)){
            return "application/msword";
        }

        if ("wk3".toLowerCase().equals(suffix)){
            return "application/x-wk3";
        }

        if ("wk4".toLowerCase().equals(suffix)){
            return "application/x-wk4";
        }

        if ("wkq".toLowerCase().equals(suffix)){
            return "application/x-wkq";
        }

        if ("wks".toLowerCase().equals(suffix)){
            return "application/x-wks";
        }

        if ("wm".toLowerCase().equals(suffix)){
            return "video/x-ms-wm";
        }

        if ("wma".toLowerCase().equals(suffix)){
            return "audio/x-ms-wma";
        }

        if ("wmd".toLowerCase().equals(suffix)){
            return "application/x-ms-wmd";
        }

        if ("wmf".toLowerCase().equals(suffix)){
            return "application/x-wmf";
        }

        if ("wml".toLowerCase().equals(suffix)){
            return "text/vnd.wap.wml";
        }

        if ("wmv".toLowerCase().equals(suffix)){
            return "video/x-ms-wmv";
        }

        if ("wmx".toLowerCase().equals(suffix)){
            return "video/x-ms-wmx";
        }

        if ("wmz".toLowerCase().equals(suffix)){
            return "application/x-ms-wmz";
        }

        if ("wp6".toLowerCase().equals(suffix)){
            return "application/x-wp6";
        }

        if ("wpd".toLowerCase().equals(suffix)){
            return "application/x-wpd";
        }

        if ("wpg".toLowerCase().equals(suffix)){
            return "application/x-wpg";
        }

        if ("wpl".toLowerCase().equals(suffix)){
            return "application/vnd.ms-wpl";
        }

        if ("wq1".toLowerCase().equals(suffix)){
            return "application/x-wq1";
        }

        if ("wr1".toLowerCase().equals(suffix)){
            return "application/x-wr1";
        }

        if ("wri".toLowerCase().equals(suffix)){
            return "application/x-wri";
        }

        if ("wrk".toLowerCase().equals(suffix)){
            return "application/x-wrk";
        }

        if ("ws".toLowerCase().equals(suffix)){
            return "application/x-ws";
        }

        if ("ws2".toLowerCase().equals(suffix)){
            return "application/x-ws";
        }

        if ("wsc".toLowerCase().equals(suffix)){
            return "text/scriptlet";
        }

        if ("wsdl".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("wvx".toLowerCase().equals(suffix)){
            return "video/x-ms-wvx";
        }

        if ("xdp".toLowerCase().equals(suffix)){
            return "application/vnd.adobe.xdp";
        }

        if ("xdr".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("xfd".toLowerCase().equals(suffix)){
            return "application/vnd.adobe.xfd";
        }

        if ("xfdf".toLowerCase().equals(suffix)){
            return "application/vnd.adobe.xfdf";
        }

        if ("xhtml".toLowerCase().equals(suffix)){
            return "text/html";
        }

        if ("xls".toLowerCase().equals(suffix)){
            return "application/x-xls";
        }

        if ("xlw".toLowerCase().equals(suffix)){
            return "application/x-xlw";
        }

        if ("xml".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("xpl".toLowerCase().equals(suffix)){
            return "audio/scpls";
        }

        if ("xq".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("xql".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("xquery".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("xsd".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("xsl".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("xslt".toLowerCase().equals(suffix)){
            return "text/xml";
        }

        if ("xwd".toLowerCase().equals(suffix)){
            return "application/x-xwd";
        }

        if ("x_b".toLowerCase().equals(suffix)){
            return "application/x-x_b";
        }

        if ("sis".toLowerCase().equals(suffix)){
            return "application/vnd.symbian.install";
        }

        if ("sisx".toLowerCase().equals(suffix)){
            return "application/vnd.symbian.install";
        }

        if ("x_t".toLowerCase().equals(suffix)){
            return "application/x-x_t";
        }

        if ("ipa".toLowerCase().equals(suffix)){
            return "application/vnd.iphone";
        }

        if ("apk".toLowerCase().equals(suffix)){
            return "application/vnd.android.package-archive";
        }

        if ("xap".toLowerCase().equals(suffix)){
            return "application/x-silverlight-app";
        }
        return "tapplication/octet-stream";
    }

    public static Boolean isImg(String suffix){

        if (
                suffix.equals("jpg")
                ||suffix.equals("jpeg")
                ||suffix.equals("png")

        ){
            return true;
        }
        return false;
    }



    public static List<String> findLine(String filePath,String toSearch) {
        List<String> searchLine = new ArrayList<>();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"UTF-8"));
            String line = null;
            String line0 = null;
            String line1 = null;
            String line2 = null;
            String line3 = null;
            String line4 = null;

            while ((line = bufferedReader.readLine())!= null){
                if (line0 == null){
                    line0 =line;
                }else {
                    if (!line0.equals(line1)){
                        line0 = line1;
                    }
                }
                if (line1 == null){
                    line1 =line;
                }else {
                    if (!line1.equals(line2)){
                        line1 = line2;
                    }
                }
                if (line2 == null){
                    line2 =line;
                }else {
                    if (!line2.equals(line3)){
                        line2 = line3;
                    }
                }
                if (line3 == null){
                    line3 =line;
                }else {
                    if (!line3.equals(line4)){
                        line3 = line4;
                    }
                }
                if (line4 == null){
                    line4 =line;
                }else {
                    if (!line4.equals(line)){
                        line4 = line;
                    }
                }
                if (
//                        line.contains(toSearch) ||
                        (line.contains("SG0021") && line.contains("47284148190725996015"))
                        || (line.contains("SG0023") && line.contains("47284148190725996015"))
                        || (line.contains("SG0022") && line.contains("47284148190725996015"))
                        || (line.contains("SG0050") && line.contains("47284148190725996015"))
                        || (line.contains("SG0006") && line.contains("<doctor_id>47284148578</doctor_id>"))
                ){
                    searchLine.add("======================================");
                    searchLine.add(line0);
                    searchLine.add(line1);
                    searchLine.add(line2);
                    searchLine.add(line3);
                    searchLine.add(line4);
                    searchLine.add("------------------------------------");
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }






        return searchLine;
    }

}
